<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{path: '/sys-serve'}">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>服务类别列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="serveId" label="服务类ID" width="120"></el-table-column>
      <el-table-column prop="serveName" label="服务类名称" width="150"></el-table-column>
      <el-table-column label="是否启用" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
              v-loading.fullscreen.lock="fullscreenLoading"
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="60">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirn(scope.row)"
                     v-loading.fullscreen.lock="fullscreenLoading"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-row style="line-height: 65px">

      <router-link to='add-new'>
      <el-button
          type="primary" round >新增<i class="el-icon-circle-plus-outline el-icon--right"></i>
      </el-button>
      </router-link>

      <el-button style="margin-left: 15px"
          type="primary" round
          @click="openFullScreen1"
          v-loading.fullscreen.lock="fullscreenLoading">刷新<i class="el-icon-refresh el-icon--right"></i>
      </el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fullscreenLoading: false,
      tableData: []
    }
  },
  methods: {
    openFullScreen1() {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
        this.loadserveList();
        this.$message({
          message: '刷新成功!!',
          type: 'success'
        });
      }, 1500);
    },
    changeEnable(serve) {
      this.fullscreenLoading = true;
      setTimeout(() => {
        this.fullscreenLoading = false;
        let enableText = ['禁用', '启用'];
        let name;
        if (serve.serveName != null) {
          name = "名称为:" + serve.serveName;
        }
        let url = 'http://localhost:9081/serve/' + serve.serveId;
        if (serve.enable == 1) {
          url += '/enable';
        } else {
          url += '/disable';
        }
        console.log('url = ' + url);
        this.axios
            .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
            .post(url).then((response) => {
          let responseBody = response.data;
          console.log(responseBody);
          if (responseBody.state == 20000) {
            this.$message({
              message: '将【' + name + '】的启用状态设置为【' + enableText[serve.enable] + '】成功!',
              type: 'success'
            });
          } else {
            // this.$message.error(responseBody.message);
            let title = '操作失败';
            this.$alert(responseBody.message, title, {
              confirmButtonText: '确定',
              callback: action => {
                this.loadserveList();
              }
            });
          }
        });
      }, 300);
    },
    handleEdit(serve) {

    },
    openDeleteConfirn(serve) {

      let message = "此操作将永久删除该项目类别, 是否继续?";
      let title = "提示";
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
      ).then(() => {
          this.handleDelete(serve);
        this.fullscreenLoading = true;
        setTimeout(() => {
          this.fullscreenLoading = false;
          this.$message({
            type: 'success',
            message: '删除成功!'
        });
        }, 1000);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除!'
        });
      });
    },
    handleDelete(serve) {
      console.log("handleDelete.... serveId=" + serve.serveId);
      let url = "http://localhost:9081/serve/" + serve.serveId + "/delete";
      console.log("url = " + url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .post(url).then((respose) => {
        let responseBody = respose.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error('您的网络出现波动!正在尝试刷新!');
        }
        this.loadserveList();
      });
    },
    loadserveList() {
      console.log('loadserveList ...');
      let url = "http://localhost:9081/serve";
      console.log(url);
      this.axios
          .create({"headers":{'Authorization':localStorage.getItem("jwt")}})
          .get(url).then((respose) => {
        let responseBody = respose.data;
        this.tableData = responseBody.data;
      })
    },
  },

  mounted() {
    this.loadserveList();
  }
}
</script>

<style scoped>

</style>